"ui";
var startTime, forwardTime, lastTime, NTPClockOffset, NTPClockDelay,startWaitTime,oneAppRunTime,
    starTkuaishou,starThuoshan,starTdouyin,starTshuabao,everydayNumber,
    ks_start_time,ks_end_time,hs_start_time,hs_end_time,dy_start_time,dy_end_time,sb_start_time,sb_end_time;
var isNeedFlash, autoPay, autoSelectAll;
var window;
var color = "#00aaff";
ui.layout(
    <drawer id="drawer">
        <vertical>
            <appbar>
                <toolbar id="toolbar" title="阅读助手"/>
            </appbar>
            <viewpager id="viewpager">
                <frame id="body" h="*" w="*">
                </frame>
            </viewpager>
        </vertical>
    </drawer>
);

// 主页
var home = {
    ui: ui.inflate(
        <ScrollView>
            <vertical padding="10">
                <text textSize="22sp" textStyle="bold" textColor="black" text="可选视频列表" w="auto" h="40" gravity="center"/>
                <horizontal h="40">
                    <checkbox id="starTkuaishou" text="快手极速版" textSize="15sp" textColor="black" h="40" checked="true"/>
                    <input id="ks_start_time" textSize="15sp" textColor="black" inputType="number"  w="auto" h="40" layout_weight="1" text="" padding="10 0 0 0" />
                    <text h="40" text=" 到 " gravity="center" textColor="black" textSize="15" />
                    <input h="40" id="ks_end_time" text="" textSize="15" w="auto" h="40" layout_weight="1" padding="10 0 0 0" layout_weight="1"/>
                    <text textSize="15sp" text="秒" textColor="black"  w="auto" h="40"/>
                </horizontal>
                <horizontal h="40">
                    <checkbox id="starThuoshan" text="火山极速版" textSize="15sp" textColor="black" h="40" checked="true"/>
                    <input id="hs_start_time" textSize="15sp" textColor="black" inputType="number"  w="auto" h="40" layout_weight="1" text="" padding="10 0 0 0" />
                    <text h="40" text=" 到 " gravity="center" textColor="black" textSize="15" />
                    <input h="40" id="hs_end_time" text="" textSize="15" w="auto" h="40" layout_weight="1" padding="10 0 0 0" layout_weight="1"/>
                    <text textSize="15sp" text="秒" textColor="black"  w="auto" h="40"/>
                </horizontal>
                <horizontal h="40">
                    <checkbox id="starTdouyin" text="抖音极速版" textSize="15sp" textColor="black" checked="true" h="40"/>
                    <input id="dy_start_time" textSize="15sp" textColor="black" inputType="number"  w="auto" h="40" layout_weight="1" text="" padding="10 0 0 0" />
                    <text h="40" text=" 到 " gravity="center" textColor="black" textSize="15" />
                    <input h="40" id="dy_end_time" text="" textSize="15" w="auto" h="40" layout_weight="1" padding="10 0 0 0" layout_weight="1"/>
                    <text textSize="15sp" text="秒" textColor="black"  w="auto" h="40"/>
                </horizontal>
                <horizontal h="40">
                    <checkbox id="starTshuabao" text="刷宝短视频" textSize="15sp" textColor="black" checked="true" h="40"/>
                    <input id="sb_start_time" textSize="15sp" textColor="black" inputType="number"  w="auto" h="40" layout_weight="1" text="" padding="10 0 0 0" />
                    <text h="40" text=" 到 " gravity="center" textColor="black" textSize="15" />
                    <input h="40" id="sb_end_time" text="" textSize="15" w="auto" h="40" layout_weight="1" padding="10 0 0 0" layout_weight="1"/>
                    <text textSize="15sp" text="秒" textColor="black"  w="auto" h="40"/>
                </horizontal>

                <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E2E2E2'></horizontal>

                <horizontal h="40">
                    <text textSize="15sp" textColor="black" text="App启动等待时间" w="auto" h="40" gravity="center_vertical" layout_weight="1"/>
                    <input id="startWaitTime" textSize="15sp" textColor="black" inputType="number" w="120" h="40"  text="10" hint="0" padding="10 0 0 0" />
                    <text textSize="15sp"  text="秒" textColor="black"  w="auto" h="40"/>
                </horizontal>

                <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E2E2E2'></horizontal>

                <horizontal h="40">
                    <text textSize="15sp" textColor="black" text="单个App运行时间" w="auto" h="40" gravity="center_vertical" layout_weight="1"/>
                    <input id="oneAppRunTime" textSize="15sp" textColor="black" inputType="number" w="120" h="40"  text="40" hint="0" padding="10 0 0 0" />
                    <text textSize="15sp"  text="分" textColor="black"  w="auto" h="40"/>
                </horizontal>

                <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E2E2E2'></horizontal>

                <horizontal h="40">
                    <text textSize="15sp" textColor="black" text="程序每日执行次数" w="auto" h="40" gravity="center_vertical" layout_weight="1"/>
                    <input id="everydayNumber" textSize="15sp" textColor="black" inputType="number" w="120" h="40"  text="4" hint="0" padding="10 0 0 0" />
                    <text textSize="15sp"  text="轮" textColor="black"  w="auto" h="40"/>
                </horizontal>

                <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E2E2E2'></horizontal>
                <horizontal h="40">
                    <Switch textSize="15sp" textColor="black" id="random_up"  text="视频随机点赞(10%几率)" checked="false" layout_weight="1" h="40"/>
                </horizontal>
                {/* 配置点赞坐标 */}
                <vertical id="radom_up_zb">
                    <horizontal h="40">
                        <text textSize="15sp" textColor="black" text="快手极速版 " w="auto" h="40" gravity="center_vertical"/>
                        <text h="40" text="X：" gravity="center" textColor="black" textSize="15"/>
                        <input h="40" id="ks_xp" text="1006" marginBottom="-6" textSize="15"  w="100" padding="10 0 0 0" layout_weight="1"/>
                        <text h="40" text=" , Y：" gravity="center" textColor="black" textSize="15" />
                        <input h="40" id="ks_yp" text="1306" marginBottom="-6" textSize="15" w="100" padding="10 0 0 0" layout_weight="1"/>
                    </horizontal>
                    <horizontal h="40">
                        <text textSize="15sp" textColor="black" text="火山极速版 " w="auto" h="40" gravity="center_vertical"/>
                        <text h="40" text="X：" gravity="center" textColor="black" textSize="15"/>
                        <input h="40" id="hs_xp" text="992" marginBottom="-6" textSize="15"  w="100" padding="10 0 0 0" layout_weight="1"/>
                        <text h="40" text=" , Y：" gravity="center" textColor="black" textSize="15" />
                        <input h="40" id="hs_yp" text="1127" marginBottom="-6" textSize="15" w="100" padding="10 0 0 0" layout_weight="1"/>
                    </horizontal>
                    <horizontal h="40">
                        <text textSize="15sp" textColor="black" text="抖音极速版 " w="auto" h="40" gravity="center_vertical"/>
                        <text h="40" text="X：" gravity="center" textColor="black" textSize="15"/>
                        <input h="40" id="dy_xp" text="996" marginBottom="-6" textSize="15"  w="100" padding="10 0 0 0" layout_weight="1"/>
                        <text h="40" text=" , Y：" gravity="center" textColor="black" textSize="15" />
                        <input h="40" id="dy_yp" text="1030" marginBottom="-6" textSize="15" w="100" padding="10 0 0 0" layout_weight="1"/>
                    </horizontal>
                    <horizontal h="40">
                        <text textSize="15sp" textColor="black" text="刷宝短视频 " w="auto" h="40" gravity="center_vertical"/>
                        <text h="40" text="X：" gravity="center" textColor="black" textSize="15"/>
                        <input h="40" id="sb_xp" text="980" marginBottom="-6" textSize="15"  w="100" padding="10 0 0 0" layout_weight="1"/>
                        <text h="40" text=" , Y：" gravity="center" textColor="black" textSize="15" />
                        <input h="40" id="sb_yp" text="910" marginBottom="-6" textSize="15" w="100" padding="10 0 0 0" layout_weight="1"/>
                    </horizontal>
                </vertical>
                <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E2E2E2'></horizontal>

                <horizontal h="40">
                    <Switch textSize="15sp" textColor="black" id="random_follow"  text="视频随机关注" checked="false" layout_weight="1" h="40"/>
                </horizontal>
                <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E2E2E2'></horizontal>
                <button id="runSrt" style="Widget.AppCompat.Button.Colored" h="50" marginTop="20" marginBorrom= "20" text="开始运行" />
                <button id="runTest" style="Widget.AppCompat.Button.Colored" h="50" marginBorrom= "20" text="获取点赞坐标" />
            </vertical>
        </ScrollView>
    ),
    activate: function () {
        setContainer(this.ui);
    }
};

home.activate();

function setContainer(v) {
    ui.body.removeAllViews();
    ui.body.addView(v, new android.widget.FrameLayout.LayoutParams(-1, -1));
}


activity.setSupportActionBar(ui.toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//需要刷新
ui.random_up.on("check", function(checked){
    if(checked){
        ui.radom_up_zb.visibility = 0x0;
        alert("警告", "当前功能需要手动获取点赞坐标，默认使用分辨率1080*1920!");
    }else{
        ui.radom_up_zb.visibility = 0x8;
    }
});

ui.runTest.on("click",function(){
    threads.start(function(){
        var Url = "https://gitee.com/wupk/auto.test/raw/master/test/touch.js";
        var res_script = http.get(Url, {
            headers: {
                'Accept-Language': 'en-us,en;q=0.5',
                'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
            }
        });
        var touch = res_script.body.string();
        // engines.execScriptFile("touch.js");
        engines.execScript("test", touch);
    });
    
});

// 获取默认开始时间
function getTime() {
    var fmt = "YYYY-MM-dd hh:mm:ss";
    var d = new Date();
    var hh = d.getHours();
    var mm = d.getMinutes();
    if (mm < 30) {
        mm = 30
    } else {
        hh += 1;
        mm = 0
    }
    var o = {
        "Y+": d.getYear() + 1900,
        "M+": d.getMonth() + 1,
        "d+": d.getDate(),
        "h+": hh,
        "m+": mm,
        "s+": 0
    };
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 4) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    fmt = fmt.replace(/-/g, '/');
    return fmt;
}

// 获取时分秒用于记录日志
function logCommon(msg) {
    console.log(formatDate(getNow()) + " " + msg);
}
function logInfo(msg) {
    console.info(formatDate(getNow()) + " " + msg);
}
function logWarn(msg) {
    console.warn(formatDate(getNow()) + " " + msg);
}
function logError(msg) {
    console.error(formatDate(getNow()) + " " + msg);
}

// 时间戳转时间字符串
function add0(m) {
    return m < 10 ? '0' + m : m
}
// 根据时间偏移值计算真实时间
function getNow() {
    var now = new Date().getTime();
    return now;
}
function add00(m) {
    if (m < 10) {
        return '00' + m;
    } else if (m < 100) {
        return '0' + m;
    } else {
        return m;
    }
}

function formatDate(needTime) {
    //needTime是整数，否则要parseInt转换
    var time = new Date(parseInt(needTime));
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return add0(h) + ':' + add0(mm) + ':' + add0(s);
}

var storage = storages.create("run_auto");
setTimeout(initConfig, 500);

function initConfig() {
    //从缓存中获取变量
    startWaitTime = storage.get("startWaitTime", "3");
    oneAppRunTime = storage.get("oneAppRunTime", "30");
    everydayNumber = storage.get("everydayNumber","4");

    ks_start_time = storage.get("ks_start_time","4");
    ks_end_time = storage.get("ks_end_time","8");
    hs_start_time = storage.get("hs_start_time","4");
    hs_end_time = storage.get("hs_end_time","8");
    dy_start_time = storage.get("dy_start_time","10");
    dy_end_time = storage.get("dy_end_time","15");
    sb_start_time = storage.get("sb_start_time","4");
    sb_end_time = storage.get("sb_end_time","8");

    ui.starTkuaishou.checked = storage.get("starTkuaishou",true);
    ui.starThuoshan.checked = storage.get("starThuoshan",true);
    ui.starTdouyin.checked = storage.get("starTdouyin",true);
    ui.starTshuabao.checked = storage.get("starTshuabao",true);

    ui.startWaitTime.setText(startWaitTime);
    ui.oneAppRunTime.setText(oneAppRunTime);
    ui.everydayNumber.setText(everydayNumber);

    ui.ks_start_time.setText(ks_start_time);
    ui.ks_end_time.setText(ks_end_time);
    ui.hs_start_time.setText(hs_start_time);
    ui.hs_end_time.setText(hs_end_time);
    ui.dy_start_time.setText(dy_start_time);
    ui.dy_end_time.setText(dy_end_time);
    ui.sb_start_time.setText(sb_start_time);
    ui.sb_end_time.setText(sb_end_time);

    ui.ks_xp.setText(storage.get("ks_xp","1006"));
    ui.ks_yp.setText(storage.get("ks_yp","1306"));
    ui.hs_xp.setText(storage.get("hs_xp","992"));
    ui.hs_yp.setText(storage.get("hs_yp","1127"));
    ui.dy_xp.setText(storage.get("dy_xp","996"));
    ui.dy_yp.setText(storage.get("dy_yp","1030"));
    ui.sb_xp.setText(storage.get("sb_xp","980"));
    ui.sb_yp.setText(storage.get("sb_yp","910"));

    ui.random_up.checked = storage.get("random_up", false);

    if(ui.random_up.isChecked()){
        ui.radom_up_zb.visibility = 0x0;
    }else{
        ui.radom_up_zb.visibility = 0x8;
    }
}

// 程序主要逻辑
ui.runSrt.click(function () {
// 检查无障碍服务
    //  if (auto.service == null) {
    //      toast("请先开启无障碍服务！");
    //      return;
    //  }
    //运行
    startWaitTime = ui.startWaitTime.getText().toString();
    oneAppRunTime = ui.oneAppRunTime.getText().toString();
    everydayNumber = ui.everydayNumber.getText().toString();

    ks_start_time = ui.ks_start_time.getText().toString();
    ks_end_time = ui.ks_end_time.getText().toString();
    hs_start_time = ui.hs_start_time.getText().toString();
    hs_end_time = ui.hs_end_time.getText().toString();
    dy_start_time = ui.dy_start_time.getText().toString();
    dy_end_time = ui.dy_end_time.getText().toString();
    sb_start_time = ui.sb_start_time.getText().toString();
    sb_end_time = ui.sb_end_time.getText().toString();

    storage.put("startWaitTime", startWaitTime);
    storage.put("oneAppRunTime", oneAppRunTime);
    storage.put("everydayNumber", everydayNumber);

    storage.put("ks_start_time", ks_start_time);
    storage.put("ks_end_time", ks_end_time);
    storage.put("hs_start_time", hs_start_time);
    storage.put("hs_end_time", hs_end_time);
    storage.put("dy_start_time", dy_start_time);
    storage.put("dy_end_time", dy_end_time);
    storage.put("sb_start_time", sb_start_time);
    storage.put("sb_end_time", sb_end_time);

    storage.get("random_up", ui.random_up.isChecked());
    storage.put("starTkuaishou",  ui.starTkuaishou.isChecked());
    storage.put("starThuoshan", ui.starThuoshan.isChecked());
    storage.put("starTdouyin", ui.starTdouyin.isChecked());
    storage.put("starTshuabao", ui.starTshuabao.isChecked());

    storage.put("ks_xp",ui.ks_xp.getText().toString());
    storage.put("ks_yp",ui.ks_yp.getText().toString());
    storage.put("hs_xp",ui.hs_xp.getText().toString());
    storage.put("hs_yp",ui.hs_yp.getText().toString());
    storage.put("dy_xp",ui.dy_xp.getText().toString());
    storage.put("dy_yp",ui.dy_yp.getText().toString());
    storage.put("sb_xp",ui.sb_xp.getText().toString());
    storage.put("sb_yp",ui.sb_yp.getText().toString());

    if (ui.runSrt.getText().toString() === '开始运行') {
        // 一直保持屏幕常亮
        device.keepScreenOn();
        threads.start(main);
        ui.runSrt.setText('停止运行');
    } else {
        threads.shutDownAll();
        console.hide();
        device.cancelKeepingAwake();
        ui.runSrt.setText('开始运行');
    }
});

var num_ = 1;
var run_ks_status = true;
var run_hs_status = true;
var run_dy_status = true;
var run_sb_status = true;
//是否开启滑动模式，已经开启不在开启，保证之开启一次，避免冲突
var is_start_slide = false;
//程序已经执行次数
var run_num = 1;
var start_run_time;
var end_run_time;
var app_status = "0";

/**
 * 主函数
 */
function main(){

    var app_run_tag = "";
    //显示控制台
    var middle = device.width / 2 - 400;
    console.setSize(800, 800);
    console.setPosition(middle, 100);
    console.show();

    logInfo("设备宽："+device.width);
    logInfo("设备高："+device.height);

    var everyday_num = parseInt(everydayNumber);

    var app_name = "";
    if(ui.starTkuaishou.isChecked()){
        app_name = "快手极速版、";
        app_run_tag = "0|";
    }
    if(ui.starThuoshan.isChecked()){
        app_name += "火山极速版、";
        app_run_tag += "1|";
    }
    if(ui.starTdouyin.isChecked()){
        app_name += "抖音极速版、";
        app_run_tag += "2|";
    }
    if(ui.starTshuabao.isChecked()){
        app_name += "刷宝短视频";
        app_run_tag += "3";
    }
    logError("本次执行的APP："+app_name);

    var tag = app_run_tag.split("|");

    //程序启动时间,不可修改
    var startTime = new Date().getTime();
    //计算程序结束时间，到达结束时间后重新初始化参数
    var end_time  = new Date( startTime + oneAppRunTime * tag.length * 60 * 1000).getTime();

    setInterval(function(){
        for (var i = 0; i < tag.length; i++) {

            var nowTime = new Date().getTime();
            var endTime = new Date(startTime.valueOf() + oneAppRunTime * i * 60 *  1000).getTime();
            var nextTime = new Date(startTime.valueOf() + oneAppRunTime * (i+1) * 60  * 1000).getTime();

            if(nowTime>=endTime && nowTime<= nextTime){
                if(tag[i].equals("0") && run_ks_status){
                    run_ks_status = false;
                    app_status = "0";
                    logInfo("运行快手极速版App");
                    start_run_time = ui.ks_start_time.getText().toString();
                    end_run_time = ui.ks_end_time.getText().toString();

                    launchApp(ui.starTkuaishou.getText());
                    threads.start(stratApp);
                }else if(tag[i].equals("1") && run_hs_status){
                    app_status = "1";
                    run_hs_status = false;
                    logInfo("运行火山极速版App");
                    start_run_time = ui.hs_start_time.getText().toString();
                    end_run_time = ui.hs_end_time.getText().toString();
                    launchApp(ui.starThuoshan.getText());
                    threads.start(stratApp);
                }else if(tag[i].equals("2") && run_dy_status){
                    app_status = "2";
                    run_dy_status = false;
                    logInfo("运行抖音极速版App");
                    start_run_time = ui.dy_start_time.getText().toString();
                    end_run_time = ui.dy_end_time.getText().toString();
                    launchApp(ui.starTdouyin.getText());
                    threads.start(stratApp);
                }else if(tag[i].equals("3")&& run_sb_status){
                    app_status = "3";
                    run_sb_status = false;
                    logInfo("运行刷宝短视频App");
                    start_run_time = ui.sb_start_time.getText().toString();
                    end_run_time = ui.sb_end_time.getText().toString();
                    launchApp(ui.starTshuabao.getText());
                    //等待app启动时间
                    var waitTime = parseInt(ui.startWaitTime.getText().toString());//这个是毫秒
                    sleep(waitTime*1000);//*1000变成秒
                    threads.start(stratApp);
                }
            }

            if(nowTime>=end_time){
                app_status = "0";
                startTime = nowTime;
                run_num++;
                end_time = new Date( nowTime.valueOf() + oneAppRunTime * tag.length * 60 * 1000).getTime();
                run_ks_status = true;
                run_hs_status = true;
                run_dy_status = true;
                run_sb_status = true;
            }

            if(run_num > everyday_num){
                logInfo("已经到达执行次数，停止程序...");
                threads.shutDownAll();
                console.hide();
                device.cancelKeepingAwake();
                ui.runSrt.setText('开始运行');
            }
        }
    },1000);
}

/**
 * 开启滑动模式
 */
function stratApp(){

    if(!is_start_slide){
        is_start_slide = true;

        setInterval(function(){
            huadong();
        },1000);
    }
}


/**
 * 滑动模式
 */
function huadong(){
    //随机等待时间random（最小值，最大值）根据需要更改数值（单位秒）
    var time_random=random(parseInt(start_run_time)*1000,parseInt(end_run_time)*1000);
    //version 1.0
    // swipe(device.width/2+200,device.height-200 ,device.width/2,height_2,duration);
    var height = parseInt(device.height);
    var width = parseInt(device.width);
    //version 1.1

    var x1 = width/2+random(100,200);
    var y1 = height-random(50,300);
    var y2 = random(100,300);
    randomSwipe(x1,y1 , x1,y2);

    logInfo("总浏览视频: "+num_+"次 ~ "+time_random);
    num_++;

    switch(app_status){
        case "0":
            kuaishou();
            break;
        case "1":
            huoshan();
            break;
        case"2":
            douyin();
            break;
        case"3":
            shuabao();
            break;
    }
    sleep(time_random);
}


//增加活跃度-点击关注，收藏，评论
function kuaishou(){
    if(className("android.widget.Button").text("继续看视频").exists()){
        className("android.widget.Button").text("继续看视频").findOnce().click();
        logInfo("执行 【继续看视频】 事件...");
    }
    if(className("android.widget.TextView").text("点击进入直播间").exists()){
        logInfo("当前视频是直播，直接跳过...");
        swipe(device.width/2+200,device.height-200 ,device.width/2,300,200);
    }
    if(className("android.widget.TextView").text("点击打开图集").exists()){
        logInfo("当前视频是图片，直接跳过...");
        swipe(device.width/2+200,device.height-200 ,device.width/2,300,200);
    }
    if(className("android.widget.TextView").text("点击打开长图").exists()){
        logInfo("当前视频是图片，直接跳过...");
        swipe(device.width/2+200,device.height-200 ,device.width/2,300,200);
    }

    if(className("android.view.View").text("立即签到").exists()){
        className("android.view.View").text("立即签到").findOnce().click();
        logInfo("执行 【立即签到】 事件...");
    }
    if(ui.random_up.isChecked()){
        var r = random(4000,8000);
        if(r<4400){
            logInfo("执行 【快手点赞事件】 事件...");
            //增加偏移量
            var x = parseInt(ui.ks_xp.getText().toString());
            x = x+random(1,4);
            var y = parseInt(ui.ks_yp.getText().toString());
            y = y+random(1,4);
            click(x,y);
        }
    }
}

function huoshan(){
    if(className("android.widget.TextView").text("点击进入直播间").exists()){
        logInfo("当前视频是直播，直接跳过...");
        swipe(device.width/2+200,device.height-200 ,device.width/2,300,200);
    }

    //活动
    if(className("android.widget.TextView").textContains("查看领取").exists()){
        // var temp = className("android.widget.TextView").textContains("查看领取").findOnce();
        // logCommon("点击事件：" + click(temp.centerX(), temp.centerY()));
        // //执行返回
        // if(id("hz").findOne().exists()){
        //     sleep(6000);
        //     id("hz").findOne().click();
        // }
        logInfo("当前视频是活动，直接跳过...");
        swipe(device.width/2+200,device.height-200 ,device.width/2,300,200);
    }
    if(ui.random_up.isChecked()){
        var r = random(4000,8000);
        if(r<4400){
            logInfo("执行 【火山点赞事件】 事件...");
            //增加偏移量
            var x = parseInt(ui.hs_xp.getText().toString());
            x = x+random(1,4);
            var y = parseInt(ui.hs_yp.getText().toString());
            y = y+random(1,4);
            click(x,y);
        }
    }
}

function douyin(){
    if(className("android.widget.TextView").text("点击进入直播间").exists()){
        logInfo("当前视频是直播，直接跳过...");
        swipe(device.width/2+200,device.height-200 ,device.width/2,300,200);
    }
    if(ui.random_up.isChecked()){
        var r = random(4000,8000);
        if(r<4400){
            logInfo("执行 【抖音点赞事件】 事件...");
            //增加偏移量
            var x = parseInt(ui.dy_xp.getText().toString());
            x = x+random(1,4);
            var y = parseInt(ui.dy_yp.getText().toString());
            y = y+random(1,4);
            click(x,y);
        }
    }
}

function shuabao(){
    if(className("android.widget.TextView").text("点击打开图集").exists()){
        logInfo("当前视频是图片，直接跳过...");
        swipe(device.width/2+200,device.height-200 ,device.width/2,300,200);
    }

    if(className("android.widget.TextView").text("点击打开长图").exists()){
        logInfo("当前视频是图片，直接跳过...");
        swipe(device.width/2+200,device.height-200 ,device.width/2,300,200);
    }
    if(ui.random_up.isChecked()){
        var r = random(4000,8000);
        if(r<4400){
            logInfo("执行 【刷宝点赞事件】 事件...");
            //增加偏移量
            var x = parseInt(ui.sb_xp.getText().toString());
            x = x+random(1,4);
            var y = parseInt(ui.sb_yp.getText().toString());
            y = y+random(1,4);
            click(x,y);
        }
    }
}


/**
 * 真人模拟滑动函数
 *
 * 传入值：起点终点坐标
 * 效果：模拟真人滑动
 */
function randomSwipe(sx, sy, ex, ey) {
    //设置控制点极限距离
    var leaveHeightLength = 300

    //根据偏差距离，应用不同的随机方式
    var my = (sy + ey) / 2
    var y2 = my + random(0, leaveHeightLength)
    var y3 = my - random(0, leaveHeightLength)

    var lx = (sx - ex) / 3
    if (lx < 0) { lx = -lx }
    var x2 = sx + lx / 2 + random(0, lx)
    var x3 = sx + lx + lx / 2 + random(0, lx)

    //获取运行轨迹，及参数
    var time = [0, random(200, 450)]
    var track = bezierCreate(sx, sy, x2, y2, x3, y3, ex, ey)
    gestures(time.concat(track))
}

/**
 * 获取滑动坐标组
 * @param {*} x1
 * @param {*} y1
 * @param {*} x2
 * @param {*} y2
 * @param {*} x3
 * @param {*} y3
 * @param {*} x4
 * @param {*} y4
 */
function bezierCreate(x1, y1, x2, y2, x3, y3, x4, y4) {
    //构建参数
    var h = 100;
    var cp = [{ x: x1, y: y1 + h }, { x: x2, y: y2 + h }, { x: x3, y: y3 + h }, { x: x4, y: y4 + h }];
    var numberOfPoints = 100;
    var curve = [];
    var dt = 1.0 / (numberOfPoints - 1);

    //计算轨迹
    for (var i = 0; i < numberOfPoints; i++) {
        var ax, bx, cx;
        var ay, by, cy;
        var tSquared, tCubed;
        var result_x, result_y;

        cx = 3.0 * (cp[1].x - cp[0].x);
        bx = 3.0 * (cp[2].x - cp[1].x) - cx;
        ax = cp[3].x - cp[0].x - cx - bx;
        cy = 3.0 * (cp[1].y - cp[0].y);
        by = 3.0 * (cp[2].y - cp[1].y) - cy;
        ay = cp[3].y - cp[0].y - cy - by;

        var t = dt * i
        tSquared = t * t;
        tCubed = tSquared * t;
        result_x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
        result_y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
        curve[i] = {
            x: result_x,
            y: result_y
        };
    }

    //轨迹转路数组
    var array = [];
    for (var i = 0; i < curve.length; i++) {
        try {
            var j = (i < 100) ? i : (199 - i);
            xx = parseInt(curve[j].x)
            yy = parseInt(Math.abs(100 - curve[j].y))
        } catch (e) {
            break
        }
        array.push([xx, yy])
    }

    return array
}